package com.sluggard.security.properties;

import cn.hutool.core.map.MapUtil;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * myabtis plus配置参数
 *
 * @author lizheng
 * @since 2024.4.17
 */
@Data
@Validated
@ConfigurationProperties(prefix = SecurityConfigConstants.SECURITY_PROPERTIES)
public class SecurityConfigProperties {

    /**
     * 免授权接口
     */
    private Set<String> permitUrls = new HashSet<>();

    /**
     * 图片验证码是否启用
     */
    private Boolean captchaEnable;

    /**
     * session 有效期
     */
    private Long sessionTimeOut;

    private Kaptcha kaptcha = new Kaptcha();

    private Olog olog = new Olog();


    public SecurityConfigProperties() {
        this.sessionTimeOut = 1800L;
        this.captchaEnable = true;
    }

    @Setter
    @Getter
    public static class Kaptcha {

        /**
         * 密码输入错误几次后启用图片验证码
         */
        private Integer errTimes = 3;

        /**
         * 验证码有效期 默认5分钟
         */
        private Long timeOut = 300L;
        /**
         * 边框
         */
        private String border = "yes";

        /**
         * 边框颜色
         */
        private String borderColor = "230,230,230";

        /**
         * 会话键
         */
        private String sessionKey = "captcha";

        /**
         * 字体
         */
        private String fontNames = "宋体,楷体,微软雅黑";

        /**
         * 字体颜色
         */
        private String fontColor = "blue";

        /**
         * 字体大小
         */
        private Integer fontSize = 30;

        /**
         * 图片宽度
         */
        private Integer width = 96;

        /**
         * 图片高度
         */
        private Integer height = 38;

        /**
         * 字符数量
         */
        private Integer charLength = 4;

        /**
         * 无噪音
         */
        private boolean noNoise = false;
    }

    @Setter
    @Getter
    public static class Olog {
        /**
         * 日志存放地址
         */
        private String storage = "D:\\media\\log";

        /**
         * 归档多少天前的数据
         */
        private Duration days = Duration.ofDays(30);

        /**
         * 从数据库每次抓取到内存的数据量
         */
        private Integer size = 1000;

        private String cron = "0 1 0 1 * ?";

        /**
         * 需要忽略的类
         */
        private List<String> ignoreClasses = new ArrayList<>();

        /**
         * 需要忽略的方法
         */
        private List<String> ignoreMethods = Arrays.asList("captcha", "menus", "roles", "getAllResourceNode", "list", "show", "tables");
        public void setIgnoreMethods(List<String> ignoreMethods) {
            this.ignoreMethods.addAll(ignoreMethods);
        }

        /**
         * 需要忽略的参数
         */
        private List<String> ignoreParams = Arrays.asList("password", "newPassword", "oldPassword");
        public void setIgnoreParams(List<String> ignoreParams) {
            this.ignoreParams.addAll(ignoreParams);
        }

        /**
         * 方法名对应注释
         */
        private Map<String, String> methodNames = MapUtil.builder(new HashMap<String, String>())
                .put("login", "登录")
                .put("logout", "退出")
                .put("changePassword", "修改密码")
                .put("changeRole", "切换角色")
                .put("save", "保存")
                .put("update", "修改")
                .put("move", "移动")
                .put("drag", "拖动")
                .put("delete", "删除")
                .put("logicalDelete", "逻辑删除").build();
        public void setMethodNames(Map<String, String> methodNames) {
            this.methodNames.putAll(methodNames);
        }
    }
}
